package com.bawei.view_animator;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyView extends View {
    private Paint paint_text,paint_hu;//画笔
    private int num;
    private int progress = 0;
    private int huColor;
    private int textColor;


    /**
     *初始化画笔：
     */
    public void init(){
        paint_text = new Paint();
        paint_text.setAntiAlias(true);//抗锯齿
        paint_text.setDither(true);//防抖动
        paint_text.setColor(textColor);
        paint_text.setTextSize(30);

        paint_hu = new Paint();
        paint_hu.setAntiAlias(true);
        paint_hu.setDither(true);
        paint_hu.setColor(huColor);
        paint_hu.setStyle(Paint.Style.STROKE);//实心 空心
        paint_hu.setStrokeWidth(20);

    }
    //Canvas：画布  Paint：画笔
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getMeasuredWidth();//当前view的宽度
        int height = getMeasuredHeight();//当前view的高度
        //绘制弧度
        RectF rectF = new RectF(20, 20, width-20, height-20);
        canvas.drawArc(rectF,0,progress,false,paint_hu);
        //绘制文字
        canvas.drawText(num+"",width/2,height/2,paint_text);
    }


    public void start(FinishListener finishListener){
        //5-0
        {
            ValueAnimator valueAnimator = ValueAnimator.ofInt(num, 0);
            valueAnimator.setDuration(num*1000);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    num = (int) animation.getAnimatedValue();
                    //重新绘制:onDraw
                    invalidate();
                    //结束
                    if(num == 0){
                        if(finishListener != null)  finishListener.onFinish();
                    }

                }
            });
            valueAnimator.start();
        }
        //0-360
        {
            ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);
            valueAnimator.setDuration(num*1000);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    progress = (int) animation.getAnimatedValue();
                    //重新绘制:onDraw
                    invalidate();

                }
            });
            valueAnimator.start();
        }

    }
    //TODO 1:定义接口
    interface FinishListener{
        void onFinish();
    }




    //new MyView()
    public MyView(Context context) {
        super(context);
        init();
    }
    //xml布局
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获得自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        num= typedArray.getInt(R.styleable.MyView_number,5);
        huColor = typedArray.getColor(R.styleable.MyView_huColor,Color.BLUE);
        textColor = typedArray.getColor(R.styleable.MyView_textColor,Color.RED);
        typedArray.recycle();//回收属性

        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }


}
